package com.cskaoyan.gateway.advice;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.user.dto.UserRegisterResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: thz
 * @class: ExceptionAdvice
 */
@ControllerAdvice
public class ExceptionAdvice {


    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseData methodArgumentNotValidException(MethodArgumentNotValidException  e){
        BindingResult bindingResult = e.getBindingResult();
        ObjectError error = bindingResult.getAllErrors().get(0);
        return new ResponseUtil().setErrorMsg(error.getDefaultMessage());
    }
}
